<?php
// +----------------------------------------------------------------------
// | Author: 闪电 <573725206@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Common\Controller\AuthController;
use Think\Auth;

Class AccessAuthController extends AuthController {
	//用户列表
	public function index() {
		$auth_group_nane = M($this->auth_group_nane);
		$auth_group_access = M($this->auth_group_access);
		//按组显示用户

		$usertablename = M($this->user_table_nane);
		$UserList = $usertablename->field('password', true)->order('id ASC')->select();

		$data['user'] = $UserList; //用户信息
		//权限组
		$group = $auth_group_nane->field('rules', true)->order('id ASC')->select();
		$this->assign('group', $group);
		$this->assign('list', $data); //用户列表

		//添置按钮
		$array[] = array('title' => '新增用户', 'url' => U('addUser'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
		$this->assign('add_button', add_button($array));

		$this->display();
	}
	//用户组
	public function group() {
		$group = M($this->auth_group_nane)->field('rules', true)->select();
		for ($i = 0; $i < count($group); $i++) {
			switch ($group[$i]['status']) {
			case '1':
				$group[$i]['status'] = '正常';
				break;

			default:
				$group[$i]['status'] = '禁用';
				break;
			}
		}
		$list['user'] = $group;
		$this->assign('list', $list);
		//添置按钮
		$array[] = array('title' => '新增权限组', 'url' => U('addgroup'), 'class' => 'primary', 'target' => 'myModal', 'type' => 'button');
		$this->assign('add_button', add_button($array));
		$this->display();

	}
	//新增组
	public function addgroup() {
		if (IS_POST) {
			$data['title'] = I('post.title');
			$data['status'] = 1;
			if (M($this->auth_group_nane)->add($data)) {
				$this->success('新增成功');
			} else {
				$this->error('操作失败');
			}
		}

	}
	//删除权限组
	public function delete_group() {
		if (IS_GET) {
			$group_access = M($this->auth_group_access)->where('group_id=' . I('get.id'))->field('group_id')->select();
			if ($group_access) {
				$this->error('该组下还有用户，请清空后再操作!');
			} else {
				if (M($this->auth_group_nane)->where('id=' . I('get.id'))->delete()) {
					$this->success('组删除成功');

				} else {
					$this->error('删除失败!');
				}

			}

		}

	}
	//锁定用户
	public function lock() {
		$map['id'] = I('get.id', '', 'intval');
		$lock = M($this->user_table_nane)->where($map)->getField('lock');
		$da['lock'] = $lock ? '0' : '1';
		$msg = $lock ? '解锁成功' : '成功禁用';
		if (M($this->user_table_nane)->where($map)->save($da)) {

			$this->success($msg);
		} else {
			$this->error($msg);
		}

	}
	//用户行为管理
	public function node() {
		$auth_rule = M($this->auth_rule);
		if (IS_POST) {

			if (I('post.name') and I('post.title')) {

				if (I('post.level') == 'Limit') {
					$this->success('添加失败,子模块下不可再添加');
				} else {
					if ($auth_rule->add($_POST)) {
						$this->success('添加成功');
					} else {
						$this->error('添回失败');
					}
				}

			} else {
				$this->error('节点地址和规则名不可为空');
			}

		} else {
			$role = $auth_rule->field('condition', true)->order('ord ASC')->select();
			//返回tree结构
			$role_nav = list_to_tree($role, 'id', 'level', 'child');
			$list['role'] = $role_nav; //规则表
			$this->assign('list', $list);
			//添置按钮
			$array[] = array('title' => '新增权限节点', 'url' => U('NodeSort'), 'class' => 'primary', 'target' => 'myModal', 'type' => 'button');
			$this->assign('add_button', add_button($array));
			$this->display();
		}

	}
	//删除权限节点
	public function del_user_role() {
		if (IS_POST) {
			$roleid = I('post.id');
			$map['id'] = $roleid;
			$map['level'] = $roleid;
			$map['_logic'] = 'OR';
			$info = M($this->auth_rule)->where($map)->field('id')->select();
			$count = count($info);
			if ($count > 1) {
				$this->error('该权限节点下还有［' . $count . ']个子节点，请删除后现操作!');
			} else {
				$requ = M($this->auth_rule)->where(array('id' => $roleid))->delete();
				if ($requ) {
					$this->success('删除成功');
				} else {
					$this->error('删除失败');
				}

			}

		} else {
			$this->error('非法操作');
		}
	}
	//添加用户
	public function addUser() {
		if (IS_POST) {
			$role_id = I('post.group_id'); //组id
			$user['username'] = I('post.username');
			$user['password'] = md5(I('post.password'));
			$user['email'] = I('post.email');
			$user['logintime'] = time(); //创建时间

			if (empty($user['username']) or empty($user['password'])) {
				$this->error('帐号或密码不能为空!');
			}
			//获取客户端ip
			$user['loginip'] = get_client_ip(); //获取当前ip

			if ($auth['uid'] = M($this->user_table_nane)->add($user)) {
				$auth['group_id'] = $role_id;
				if (M($this->auth_group_access)->add($auth)) {
					$this->success('添加成功', U('index'));
				} else {
					//设置组权限出错，删除已经填加的用户
					M($this->user_table_nane)->where('id=' . $auth['uid'])->delete();
					$this->error('添加出错!');
				}
			} else {
				$this->error('添加失败!');
			}

		} else {

			$this->display();
		}

	}

	//配置权限
	public function group_access() {
		if (IS_POST) {
			$roleid = I('post.roleid');
			$id = I('post.id');

			$auth_group_nane = M($this->auth_group_nane);
			$rules['rules'] = array_string($roleid);

			if ($auth_group_nane->where(array('id' => $id))->save($rules)) {
				$this->success('配置权限成功');
			} else {
				$this->error('操作失败!');
			}
		} else {

			$auth_rule = M($this->auth_rule);
			$role = $auth_rule->field('condition', true)->order('ord ASC')->select();

			$role_nav = $this->_Role_Nav($role, I('get.id'), $this->auth_group_nane);

			$list['role'] = $role_nav; //规则表
			$this->assign('list', $list);
			$this->display();
		}

	}

	//修改用户资料
	public function romUser() {
		$user_table_name = M($this->user_table_nane);
		$auth_group_nane = M($this->auth_group_nane);
		if (IS_POST) {
			$data = I('post.');

			//修改用户
			if (!$data['password']) {
				unset($data['password']);
			} else {
				$data['password'] = md5($data['password']);
			}

			$postid = I('post.id');

			if ($user_table_name->where(array('id' => $postid))->save($data)) {
				$group_id['group_id'] = I('post.group_id');
				if (!$group_id['group_id']) {
					$this->success('修改成功!');
					exit;
				}
				if (M($this->auth_group_access)->where(array('uid' => $postid))->save($group_id)) {
					$this->success('修改成功!');
				} else {
					$this->error('用户修改成功,修改用户所在组权限失败!');
				}

			} else {
				$this->error('修改失败!');
			}

		} else {
			$id = I('get.id'); //用户id
			$userinfo = $user_table_name->where('id=' . $id)->field('password', true)->select();
			//权限组
			$user = $auth_group_nane->field('status', true)->select();
			//查询出用户所在组
			$AgeList = new Auth();
			$age = $AgeList->getGroups($id);
			//设置选中状态
			for ($i = 0; $i < count($user); $i++) {
				if ($user[$i]['id'] == $age['0']['group_id']) {
					$user[$i]['selected'] = "selected='selected'";
				}
			}
			$list['group'] = $user;
			$list['userinfo'] = $userinfo;
			$this->assign('list', $list);

			$this->display();
		}

	}

	//删除用户
	public function DelUser() {
		if (IS_GET) {
			$getid = I('get.id');
			if (M($this->user_table_nane)->where(array('id' => $getid))->delete()) {
				//删除用户所在组权限
				if (M($this->auth_group_access)->where(array('uid' => $getid))->delete()) {
					$this->success('删除成功');
				} else {
					$this->error('删除失败');
				}

			} else {
				$this->error('删除失败');

			}
		}

	}
	//用户行为排序
	public function NodeSort() {
		$data['ord'] = I('post.val');
		$return = M($this->auth_rule)->where(array('id' => I('post.id')))->save($data);
		if ($return) {
			$json['status'] = true;
			$json['content'] = '更改成功';
		} else {
			$json['status'] = false;
			$json['content'] = '未变更';
		}
		$this->ajaxReturn($json);
	}

}
